const app = getApp();
let deleteIndex = ""; //删除的索引
const statusList = [{
  id: 0,
  label: '全部'
}, {
  id: 1,
  label: '在线'
}, {
  id: 2,
  label: '离线'
}];
const noticeList = [{
    id: '',
    label: '全部'
  },
  {
    id: 1,
    label: '正常'
  }, {
    id: 2,
    label: '低水位'
  }, {
    id: 3,
    label: '低压'
  }, {
    id: 4,
    label: '高压'
  }, {
    id: 5,
    label: '机箱内温度低'
  },
  {
    id: 6,
    label: '机箱内温度高'
  }
];

Page({
  /**
   * 页面的初始数据
   */
  data: {
    drawer: false, //展开搜索抽屉
    isFilter: false, // 是否有筛选条件
    modal: false,
    modalContent: "",
    warning_icon: '/images/warning.png',
    navigation_icon: '/images/navigation.png',
    delete_icon: '/images/del.png',
    edit_icon: '/images/edit.png',
    searchImg: '/images/search.png',
    page: 1, //当前页数
    pageSize: 20,
    equipmentList: [],
    empty: false,
    loadmore: false,
    nomore: false,
    equipment_keyword: '',
    start_time: '',
    end_time: '',
    statusList,
    statusIndex: 0,
    noticeList,
    noticeIndex: 0,
    isRepair: false, //是否是维修员
    isfranchiser: false,

  },
  onUnload: function () {
    deleteIndex = "";
  },

  showDrawer() {
    this.condition = {
      equipment_keyword: this.data.equipment_keyword,
      noticeIndex: this.data.noticeIndex,
      statusIndex: this.data.statusIndex,
      start_time: this.data.start_time,
      end_time: this.data.end_time
    }
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      ...this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    this.setData({
      equipment_keyword: "",
      noticeIndex: "",
      statusIndex: "",
      start_time: "",
      end_time: ""
    })
  },
  confirmDrawer() {
    let isFilter = false;
    if (this.data.equipment_keyword != "" ||
      this.data.noticeIndex !== "" ||
      this.data.statusIndex !== "" ||
      this.data.start_time != "" ||
      this.data.end_time != ""
    ) {
      isFilter = true;
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


  navParamSetting(e) {
    wx.navigateTo({
      url: '/package/pages/parameterSetting/index?sn=' + e.currentTarget.dataset.sn,
    })
  },

  //跳转到设备二维码
  navThrathQrcode(e) {
    wx.navigateTo({
      url: '../moreInformation/deviceQrcode?eq_sn=' + e.currentTarget.dataset.sn,
    })
  },

  copyEqiupmentNumber(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
    })
  },

  //隐藏自定义modal
  hideModal() {
    deleteIndex = "";
    this.setData({
      modal: false
    })
  },

  //展示modal
  showModal(e) {
    deleteIndex = e.currentTarget.dataset.index;
    const list = this.data.equipmentList;
    this.setData({
      modalContent: `是否删除设备${list[deleteIndex].equipment_name}`,
      modal: true
    })
  },

  //操作modal的按钮
  handleModalClick(e) {
    if (deleteIndex === "") {
      app.showToast("请先选择删除的栏目");
      return;
    } else {
      const index = e.detail.index;
      if (index === 0) {
        this.hideModal();
      } else if (index === 1) {
        const list = this.data.equipmentList;
        const id = list[deleteIndex].id;
        app.ajax({
          url: 'Partner/Equipment/deleteEquipment',
          data: {
            equipment_id: id,
          },
          success: res => {
            if (res.data.code == 1000) {
              app.showToast(res.data.message, "success");
              this.hideModal();
              setTimeout(() => {
                this.reloadData();
              }, 1500)
            } else {
              app.showToast(res.data.message)
            }
          }
        })
      }
    }
  },


  onLoad: function (options) {
    this.setData({
      isfranchiser: (options && options.isfranchiser) || 0,
      isRepair: (options && options.is_repair) || false, //维修员
      param: options.param || 0, // 维修员的情况下 0总设备 1正常 2异常 3在线 4离线
    })
    if (this.data.isRepair && this.data.param > 0) {
      let param = Number.parseInt(this.data.param);
      let arr = ["正常设备", "异常设备", "在线设备", "离线设备"];
      wx.setNavigationBarTitle({
        title: arr[param - 1],
      })
    }
    wx.showLoading({
      title: '加载中'
    });
    this.getEquipmentList();
  },

  reloadData() {
    this.setData({
      equipmentList: [],
      page: 1,
      loadmore: false,
      nomore: false,
      empty: false,
      offline: 0,
      total: 0,
      online: 0
    })
    wx.showLoading({
      title: '加载中'
    });
    this.getEquipmentList();
  },


  onPullDownRefresh() {
    this.reloadData();
  },

  onReachBottom() {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getEquipmentList();
    }
  },


  //获取设备列表
  getEquipmentList() {
    let notice = '',
      status = '';
    if (this.data.noticeIndex !== '') {
      notice = this.data.noticeList[this.data.noticeIndex].id;
    }
    if (this.data.statusIndex !== '') {
      status = this.data.statusList[this.data.statusIndex].id;
    }
    const params = {
      page: this.data.page,
      pageSize: this.data.pageSize,
      equipment_keyword: this.data.equipment_keyword,
      equipment_status: notice,
      status: status,
      start_time: this.data.start_time,
      end_time: this.data.end_time
    }
    if (this.data.isRepair && this.data.param > 0) {
      if (this.data.param == 3) {
        params.status = 1;
      } else if (this.data.param == 4) {
        params.status = 2;
      } else if (this.data.param == 1) {
        params.normal_status = true;
      } else if (this.data.param == 2) {
        params.normal_status = false;
      }
    }
    if (this.data.isfranchiser) {
      params.isfranchiser = 1;
      params.type = 1
    }
    let url = "Partner/Equipment/getList";
    if (this.data.isRepair) {
      url = "Manage/Equipment/getList";
    }
    app.ajax({
      url,
      data: params,
      success: res => {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        if (res.data.code > 0) {
          let source = res.data.data;
          let list = this.data.equipmentList.concat(source.eq);
          this.setData({
            equipmentList: list,
            empty: source.count == 0 ? true : false,
            offline: source.off_number || 0,
            online: source.on_number || 0,
            total: source.count || 0,
            loadmore: list.length < source.count ? true : false,
            nomore: list.length >= source.count ? true : false,
            drawer: false,
          })
        } else {
          let empty = false;
          if (this.data.page == 1) {
            this.setData({
              equipmentList: [],
            })
            empty = true;
          }
          this.setData({
            empty,
            loadmore: false,
            nomore: true,
            drawer: false,
          })
        }
      },
      error: e => {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        app.showToast("网络错误");
      }
    })
  },


  //编辑
  edit(e) {
    let url = '../equipmentEdit/equipmentEdit?type=2&equipment_id=' + e.currentTarget.dataset.equipment_id;
    if (this.data.isfranchiser == 1) {
      url += "&is_franchiser=1"
    }
    if (this.data.isRepair) {
      url += "&is_repair=1"
    }
    wx.navigateTo({
      url
    })
  },

  //添加设备
  addNewEq() {
    let url = "../equipmentEdit/equipmentEdit?type=1";
    if (this.data.isfranchiser) {
      url += '&is_franchiser=1';
    }
    if (this.data.isRepair) {
      url += "&is_repair=1";
    }
    wx.navigateTo({
      url,
    })
  },

  //设备导航
  goMap(e) {
    let lat = e.currentTarget.dataset.lat;
    let lng = e.currentTarget.dataset.lng;
    wx.navigateTo({
      url: '../equipmentMap/equipmentMap?lat=' + lat + '&lng=' + lng,
    })
  },

  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [name]: index === this.data[`${name}`] ? '' : index
    })
  },

  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    });
  },


  //水卡
  waterCard(e) {
    wx.navigateTo({
      url: '../waterCard/waterCard?equipment_id=' + e.currentTarget.dataset.equipment_id,
    })
  },
  //区域码
  areaCode(e) {
    let url = '/package/pages/areaCode/areaCodeList/areaCodeList?id=' + e.currentTarget.dataset.id;
    if (this.data.isfranchiser) {
      url += "&is_franchiser=1";
    }
    if (this.data.isRepair) {
      url += "&is_repair=1";
    }
    wx.navigateTo({
      url
    })
    // if (this.data.isRepair) {

    // } else {
    //   wx.navigateTo({
    //     url: '/package/pages/areaCode/areaCodeList/areaCodeList?id=' + e.currentTarget.dataset.id,
    //   })
    // }
  },
  //消费记录 
  consumeHistory(e) {
    let url = `../order/order?type=1&id= ${e.currentTarget.dataset.id}`;
    if (this.data.isRepair) {
      url += '&is_repair=1'
    }
    wx.navigateTo({
      url,
    })
  },
  //更多信息
  moreInformation(e) {
    let url = `../moreInformation/moreInformation?equipment_id=${e.currentTarget.dataset.equipment_id}`;
    if (this.data.isRepair) {
      url += "&is_repair=1"
    }
    if (this.data.isfranchiser) {
      url += "&is_franchiser=1"
    }
    wx.navigateTo({
      url,
    })
  },
})